<?php
class Filter {
	private static $filterList = array();
	private static $filtersOnPage = array();

	public static function apply($var, $filterName){
		if (DEV_MODE) {
			self::$filtersOnPage[] = $filterName;
		}

		if (isset(self::$filterList[$filterName])) {
			foreach (self::$filterList[$filterName] as $v => $array) {
				$var = call_user_func_array( $array[0], array_merge(array($var), $array[1]));
			}
		}

		return $var;
	}

	public static function setFilter($filterName,$handler, array $params = array()){
		self::$filterList[$filterName][callback_to_string($handler)] = array($handler,$params);
	}

	public static function deleteFilter($eventName,$handler){
		unset(self::$filterList[$eventName][callback_to_string($handler)]);
	}

	public static function getFilters($masc = false){
		if (!DEV_MODE) return;
		Event::setEventHandler('end',array(__CLASS__,'_traceFilters'),array($masc));
	}

	public static function _traceFilters($masc){
		self::$filtersOnPage = array_values(array_unique(self::$filtersOnPage));

		if ( $masc ){
			$tempArr = array();

			foreach (self::$filtersOnPage as $filter) {
				if (preg_match($masc,$filter)) {
					$tempArr[] = $filter;
				}
			}

			self::$filtersOnPage = $tempArr;
		}

		debug(self::$filtersOnPage);
	}
}
